﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using CPAScoringPerformanceTester.TestTask;
using CPAScoringPerformanceTester.Models;

namespace CPAScoringPerformanceTester.Windows
{
    /// <summary>
    /// TaskWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TaskWindow : Window, INotifyPropertyChanged
    {
        private TestTaskItem taskItemType;
        public event PropertyChangedEventHandler PropertyChanged;
        public event TaskSelectedEventHandler TaskSelected;

        public ObservableCollection<TestTaskCandidate> TaskItemTypes { get; set; }
        public RelayCommand ApplyCommand { get; set; }
        public RelayCommand CloseCommand { get; set; }

        public TaskWindow(TestTaskItem taskItemType)
        {
            this.taskItemType = taskItemType;
            TaskItemTypes = new ObservableCollection<TestTaskCandidate>();
            ApplyCommand = new RelayCommand(Apply);
            CloseCommand = new RelayCommand(() => this.Close());
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(TaskWindow_Loaded);
        }

        void TaskWindow_Loaded(object sender, RoutedEventArgs e)
        {
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ServiceConnection, TaskName = ServiceConnectionTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.UserLogin, TaskName = UserLoginTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskList, TaskName = ScoreTaskListTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskDetail, TaskName = ScoreTaskDetailTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskPaperItems, TaskName = ScoreTaskPaperItemsTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskApply, TaskName = ScoreTaskApplyTask.TaskName });
            //TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskRecord, TaskName = ScoreTaskRecordTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskSave, TaskName = ScoreTaskSaveTask.TaskName });
            TaskItemTypes.Add(new TestTaskCandidate() { TaskItemType = TestTaskItem.ScoreTaskSubmit, TaskName = ScoreTaskSubmitTask.TaskName });
            SetDefault();
        }

        private void SetDefault()
        {
            foreach (var item in TaskItemTypes)
            {
                if (item.TaskItemType == taskItemType)
                {
                    item.IsChecked = true;
                    break;
                }
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void OnTaskSelected(TestTaskItem taskType)
        {
            if (TaskSelected != null)
            {
                TaskSelected(this, new TaskSelectedEventArgs(taskType));
            }
        }

        private void Apply()
        {
            foreach (var item in TaskItemTypes)
            {
                if (item.IsChecked)
                {
                    OnTaskSelected(item.TaskItemType);
                    break;
                }
            }
            this.Close();
        }
    }

    public class TestTaskCandidate
    {
        public bool IsChecked { get; set; }
        public TestTaskItem TaskItemType { get; set; }
        public string TaskName { get; set; }
    }
}
